package com.appbee.media.client.ui;

import com.appbee.core.client.upload.File;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.Widget;

public class MultiFileInput extends Widget
{
	public MultiFileInput()
	{
		setElement(Document.get().createFileInputElement());
		getElement().setAttribute("multiple", "");
		getElement().setAttribute("name", "myFile");
	}
	
	public HandlerRegistration addChangeHandler(ChangeHandler handler) {
	    return addDomHandler(handler, ChangeEvent.getType());
	  }
	
	public File[] getFiles()
	{
		JsArray<File> files = getFiles(getElement());
		
		int length = files.length();
		File[] f = new File[length];
		for(int i=0;i<length;i++)
		{
			f[i] = files.get(i);
		}
		return f;
	}
	
	private final native JsArray<File> getFiles(Element el)/*-{
		return el.files;
	}-*/;
}
